import __config from "../../../../config/env";
const app = getApp()

Page({
  data: {
    orderItem: {},
    orderRefunds: {},
    picUrls: []
  },
  onShow() {},
  onLoad(options) {
    this.setData({
      orderItemId: options.orderItemId,
      [`orderRefunds.orderItemId`]: options.orderItemId,
      [`orderRefunds.status`]: '1'
    })
    app.initPage()
      .then(res => {
        this.orderItemGet(this.data.orderItemId)
      })
  },
  orderItemGet(id) {
    let that = this
    app.api.orderItemGet(id)
      .then(res => {
        let orderItem = res.data
        this.setData({
          orderItem: orderItem
        })
      })
  },
  resonInput(e) {
    this.setData({
      [`orderRefunds.refundReson`]: e.detail.value
    })
  },
  radioChange(e) {
    this.setData({
      [`orderRefunds.status`]: e.detail.value
    })
  },
  subRefunds() {
    if (!this.data.orderRefunds.refundReson) {
      wx.showToast({
        title: '请输入退款原因',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.picUrls.length == 0) {
      wx.showToast({
        title: '请上传退款凭证',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let that = this
    let orderRefunds = that.data.orderRefunds
    orderRefunds["picUrls"] = that.data.picUrls
    wx.showModal({
      content: '确认提交退款申请吗？',
      cancelText: '我再想想',
      confirmColor: '#ff0000',
      success(res) {
        if (res.confirm) {
          app.api.orderRefundsSave(orderRefunds)
            .then(res => {
              wx.redirectTo({
                url: '/pages/order/order-refunds/form/index?orderItemId=' + that.data.orderItemId
              })
            })
        }
      }
    })
  },
  handleRefunds() {
    wx.redirectTo({
      url: '/pages/order/order-refunds/form/index?orderItemId=' + this.data.orderItemId
    })
  },
  ChooseImage() {
    const that = this
    wx.chooseMedia({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], //从相册选择
      mediaType: ['image'],
      success: (res) => {
        let filePath = res.tempFiles[0].tempFilePath
        wx.showLoading({
          title: '加载中',
        })
        // 上传图片
        wx.uploadFile({
          url: __config.basePath + '/mall/api/ma/file/upload',
          filePath: filePath,
          header: {
            'app-id': wx.getAccountInfoSync().miniProgram.appId,
            'third-session': wx.getStorageSync('third_session') != null ? wx.getStorageSync('third_session') : ''
          },
          name: 'file',
          formData: {
            'fileType': 'image',
            'dir': 'order/refunds/'
          },
          success(uploadFileRes) {
            if (uploadFileRes.statusCode == '200') {
              let link = JSON.parse(uploadFileRes.data).link
              let picUrls = that.data.picUrls
              if (picUrls) {
                picUrls.push(link)
              } else {
                picUrls = []
                picUrls.push(link)
              }
              that.setData({
                picUrls: picUrls
              })
            } else {
              wx.showModal({
                title: '提示',
                content: '上传失败：' + uploadFileRes.data,
                success(res) {}
              });
            }
          },
          fail: (err) => {
            console.log(err)
          },
          complete(res) {
            wx.hideLoading()
          }
        })
      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.picUrls,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '提示',
      content: '确定删除？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.picUrls.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            picUrls: this.data.picUrls
          })
        }
      }
    })
  },
})